<script>
// // #ifdef MP-WEIXIN
// const miniShopPlugin = requirePlugin('mini-shop-plugin');
// // #endif
import { request } from "./utils/request";
import API from "@/config/api";
import store from "./store";

export default {
  onLaunch: function (options) {
    console.log("onLaunch 开始...");
    if (
      options &&
      options.path === "zwx_category_page1/goodsModule/goodsDetails" &&
      options.query
    ) {
      this.globalData.productShareItem = options.query;
    }
    if (
      options &&
      options.path === "zwx_category_page1/store/index" &&
      options.query
    ) {
      this.globalData.shopShareItem = options.query;
    }
    if (
      options &&
      options.path === "zwx_category_page1/distributionModule/recruit" &&
      options.query
    ) {
      this.globalData.distributeRecruitItem = options.query;
    }
    if (
      options &&
      options.path === "zwx_category_page1/goodsModule/inviteSpell" &&
      options.query
    ) {
      this.globalData.inviteSpellShareItem = options.query;
    }
    //判断设备是否为 iPhone
    const self = this;
    uni.getSystemInfo({
      success: function (res) {
        // console.log(res, "检查机型");
        // 根据 model 进行判断
        // const iphoneModels = [
        // 	'iPhone X',
        // 	'iPhone XR',
        // 	'iPhone XS Max',
        // 	'iPhone 11',
        // 	'iPhone 11 Pro',
        // 	'iPhone 11 Pro Max',
        // 	'iPhone 12/13 (Pro)',
        // 	'iPhone 12/13 mini',
        // 	'iPhone 12/13 Pro Max'
        // ]
        if (res.safeArea.top > 20 && res.model.indexOf("iPhone") !== -1) {
          self.globalData.isIphone = true;
        }

        const platform = res.uniPlatform; // 平台信息
        console.log("uni平台=", platform)
        // 获取tabbar与navbar高度
        let temp_tabBarHeight = 0;
        if (platform === "web") {
          temp_tabBarHeight = 50;

        } else {
          const screenHeight = res.screenHeight; // 屏幕总高度
          const windowHeight = res.windowHeight; // 可用窗口高度
          const safeAreaInsets = res.safeAreaInsets || { bottom: 0 }; // 底部安全区域高度

          const tabBar_height =
            screenHeight - windowHeight - safeAreaInsets.bottom;
          temp_tabBarHeight = tabBar_height > 0 ? tabBar_height : 0;
        }

        console.log("tabbar高度=", temp_tabBarHeight);

        // 计算导航栏高度
        let temp_navBarHeight = 0;
        const statusBarHeight = res.statusBarHeight || 0; // 状态栏高度
        if (platform === "web") {
          // 其他平台的导航栏高度
          temp_navBarHeight = statusBarHeight + 44; // 默认高度
        } else {
          // 仅微信小程序支持 getMenuButtonBoundingClientRect
          const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
          const menuButtonHeight = menuButtonInfo.height || 0;
          console.log("胶囊行高度", menuButtonHeight)
          temp_navBarHeight =
            statusBarHeight +
            (menuButtonInfo.top - statusBarHeight) * 2 +
            menuButtonHeight;
            console.log(menuButtonInfo.top, statusBarHeight)
          // temp_navBarHeight = statusBarHeight + menuButtonHeight
        }
        console.log("navbar高度=", temp_navBarHeight, "，statusBar高度=", statusBarHeight);

        store.commit("globalSetting/updateData", {
          key: "appSetting",
          obj: {
            tabBarHeight: temp_tabBarHeight,
            navBarHeight: temp_navBarHeight,
          },
        });
      },
      fail: (err) => {
        console.error("获取系统信息失败:", err);
      },
    });
    // 购物车右上角数量
    if (uni.getStorageSync("storage_key")) {
      request(API.ShoppingCart, {}, "GET").then((resCart) => {
        let cartNum = 0;
        resCart.data.forEach((shopItem) => {
          shopItem.skus.forEach((goodsItem) => {
            cartNum += goodsItem.number;
          });
        });
        uni.setStorageSync("allCartNum", cartNum);
        if (cartNum > 0) {
          uni.setTabBarBadge({
            index: 3,
            text: cartNum.toString(),
          });
        }
      });
    }

    /*
      在页面中使用app.globalInitPromise，可以保证onLaunch优先执行.
      举例：
      onMounted(async () => {
        const app = getApp();
        app.globalInitPromise && await app.globalInitPromise;
        fn();
      });
    */
    this.globalInitPromise = this.initialize();
  },
  globalData: {
    isIphone: false,
  },
  // 全局存储 Promise，用户判断onLaunch是否执行完成
  globalInitPromise: null,
  methods: {
    // px单位的数值转成rpx单位的数值
    px2rpx(val) {
      const systemInfo = uni.getSystemInfoSync();
      if (typeof val == "string") {
        const temp = Number(val.match(/\d+(\.\d+)?/)[0]);
        return temp * (750 / systemInfo.screenWidth);
      }
      if (typeof val == "number") {
        return val * (750 / systemInfo.screenWidth);
      }
    },
    // rpx单位的数值转成px单位的数值
    rpx2px(val) {
      if (typeof val == "string") {
        const temp = Number(val.match(/\d+(\.\d+)?/)[0]);
        return uni.upx2px(temp);
      }
      if (typeof val == "number") {
        return uni.upx2px(val);
      }
    },
    async initialize() {
      // 模拟异步操作，例如获取用户信息
      await new Promise((resolve) => {
        // 可以做一些需要在onLaunch启动时操作的步骤
      });
      console.log("onLaunch 结束...");
    },
  },
};
</script>

<style lang="scss">
@import "./style/images";
@keyframes loading {
  0% {
    background: #e7e7e7;
  }

  50% {
    background: #f8f8f8;
  }

  100% {
    background: #e7e7e7;
  }
}

// 自定义骨架屏
.ske-loading {
  .child-loading {
    animation: loading 2s linear 0s infinite alternate;
  }
}

.ske-loading-box {
  padding: 5rpx 20rpx;
  box-sizing: border-box;

  .ske-loading-item {
    margin: 20rpx 0;
  }
}

/*每个页面公共css */
@import "./uni_modules/vk-uview-ui/index.scss";

uni-rich-text img {
  max-width: 100% !important;
}

// 图片占位图
.pic-img {
  width: 100%;
  height: 100%;
}

.default-img {
  background: url('#{$base-path}/ui-static/img/default.png') no-repeat center;
  background-size: 100% 100%;
}

.line1 {
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}

.line2 {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  word-break: break-all;
  white-space: normal;
}

.wid {
  width: 100%;
}

.fs4 {
  font-size: 4upx;
}

.fs18 {
  font-size: 18upx;
}

.fs20 {
  font-size: 20upx;
}

.fs22 {
  font-size: 22upx;
}

.fs24 {
  font-size: 24upx;
}

.fs26 {
  font-size: 26upx;
}

.fs28 {
  font-size: 28upx;
}

.fs30 {
  font-size: 30upx;
}

.fs32 {
  font-size: 32upx;
}

.fs34 {
  font-size: 34upx;
}

.fs36 {
  font-size: 36upx;
}

.fs38 {
  font-size: 38upx;
}

.fs40 {
  font-size: 40upx;
}

.fs42 {
  font-size: 42upx;
}

.fs44 {
  font-size: 44upx;
}

.fs46 {
  font-size: 46upx;
}

.fs48 {
  font-size: 46upx;
}

.fs50 {
  font-size: 50upx;
}

.fs60 {
  font-size: 60upx;
}

.fs-bold {
  font-weight: bold;
}

.fs-weight-300 {
  font-weight: 300;
}

.fs-weight-200 {
  font-weight: 200;
}

.fs-weight-400 {
  font-weight: 400;
}

.flex-display,
.flex {
  display: flex;
}

.flex-wrap {
  flex-wrap: wrap;
}

.flex-1 {
  flex: 1;
}

.flex-center,
.justify-center {
  display: flex;
  justify-content: center;
}

.flex-items,
.items-center {
  display: flex;
  align-items: center;
}

.flex-items-plus {
  display: flex;
  justify-content: center;
  align-items: center;
}

.flex-start {
  display: flex;
  justify-content: flex-start;
}

.flex-end {
  display: flex;
  justify-content: flex-end;
}

.flex-end-plus {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.flex-column {
  flex-direction: column;
}

.flex-column-plus {
  display: flex;
  flex-direction: column;
}

.flex-row {
  flex-direction: row;
}

.flex-row-plus {
  display: flex;
  flex-direction: row;
}

.flex-sp-around,
.justify-between {
  justify-content: space-around;
}

.flex-sp-between,
.justify-between {
  justify-content: space-between;
}

.text-align {
  text-align: center;
}

.flex-wrap-1 {
  display: flex;
  flex-wrap: wrap;
}

.flex-nowrap-1 {
  display: flex;
  flex-wrap: nowrap;
}

.align-end {
  display: flex;
  align-items: flex-end;
}

.align-sp-between {
  align-content: space-between;
}

.mar-top-5 {
  margin-top: 5upx;
}

.mar-top-10 {
  margin-top: 10upx;
}

.mar-top-20 {
  margin-top: 20upx;
}

.mar-top-30 {
  margin-top: 30upx;
}

.mar-top-32 {
  margin-top: 32upx;
}

.mar-top-36 {
  margin-top: 36upx;
}

.mar-top-40 {
  margin-top: 40upx;
}

.mar-top-50 {
  margin-top: 50upx;
}

.mar-top-60 {
  margin-top: 60upx;
}

.mar-top-70 {
  margin-top: 70upx;
}

.mar-top-100 {
  margin-top: 100upx;
}

.mar-top-percent40 {
  margin-top: 40%;
}

.mar-top-half {
  margin-top: 50%;
}

.mar-left-6 {
  margin-left: 6upx;
}

.mar-left-5 {
  margin-left: 5upx;
}

.mar-left-10 {
  margin-left: 10upx;
}

.mar-left-20 {
  margin-left: 20upx;
}

.mar-left-30 {
  margin-left: 30upx;
}

.mar-left-35 {
  margin-left: 35upx;
}

.mar-left-40 {
  margin-left: 40upx;
}

.mar-left-50 {
  margin-left: 50upx;
}

.mar-left-60 {
  margin-left: 60upx;
}

.mar-left-70 {
  margin-left: 70upx;
}

.mar-right-10 {
  margin-right: 10upx;
}

.mar-right-20 {
  margin-right: 20upx;
}

.mar-right-25 {
  margin-right: 25upx;
}

.mar-right-30 {
  margin-right: 30upx;
}

.mar-right-35 {
  margin-right: 35upx;
}

.mar-right-40 {
  margin-right: 40upx;
}

.mar-right-50 {
  margin-right: 50upx;
}

.pad-left-10 {
  padding-left: 10upx;
}

.pad-left-20 {
  padding-left: 20upx;
}

.pad-left-40 {
  padding-left: 40upx;
}

.pad-right-20 {
  padding-right: 20upx;
}

.pad-top-20 {
  padding-top: 20upx;
}

.pad-top-40 {
  padding-top: 40upx;
}

.pad-bot-20 {
  padding-bottom: 20upx;
}

.pad-topbot-20 {
  padding: 20upx 0upx;
}

.pad-topbot-5 {
  padding: 0upx 5upx;
}

.pad-topbot-10 {
  padding: 0upx 10upx;
}

.pad-topbot-50 {
  padding: 50upx 0upx;
}

.pad-bot-20 {
  padding-bottom: 20upx;
}

.pad-bot-30 {
  padding-bottom: 30upx;
}

.pad-bot-40 {
  padding-bottom: 40upx;
}

.pad-bot-100 {
  padding-bottom: 100upx;
}

.pad-bot-140 {
  padding-bottom: 140upx;
}

.bor-rad-30 {
  border-radius: 30upx;
}

.bor-rad-45 {
  border-radius: 45upx;
}

.bor-rad-half {
  border-radius: 50%;
}

.backColor {
  background-color: #009688;
}

.backColorFFF {
  background-color: #ffffff;
}

.pos-rel,
.relative {
  position: relative;
}

.pos-abs,
.absolute {
  position: absolute;
}

.bor-bot-line {
  border-bottom: #c8c7cc 1upx solid;
}

.bor-line-F7F7F7 {
  border-bottom: #f7f7f7 1upx solid;
}

.bor-line-E5E5E5 {
  border-bottom: #e5e5e5 1upx solid;
}

.borRig-line-E5E5E5 {
  border-right: #dddddd 2upx solid;
}

.borRig-line-20 {
  border-bottom: #f7f7f7 20upx solid;
}

.font-color-red {
  color: red;
}

.font-color-FFF {
  color: #ffffff;
}

.font-color-8A734A {
  color: #8a734a;
}

.font-color-71521B {
  color: #71521b;
}

.font-color-222 {
  color: #222222;
}

.font-color-333 {
  color: #333333;
}

.font-color-666 {
  color: #666666;
}

.font-color-999 {
  color: #999999;
}

.font-color-656 {
  color: #656565;
}

.font-color-DDD {
  color: #dddddd;
}

.font-color-CCC {
  color: #cccccc;
}

.font-color-FFEBC4 {
  color: #ffebc4;
}

.font-color-1CC363 {
  color: #1cc363;
}

.font-color-47A7EE {
  color: #47a7ee;
}

.font-color-C5AA7B {
  color: #c5aa7b;
}

.font-color-FF7700 {
  color: #ff7700;
}

.font-color-FF7911 {
  color: #ff7911;
}

.font-color-80 {
  color: #808080;
}

.font-color-DD {
  color: #dd524d;
}

.font-color-C83732 {
  color: #EF4444;
}

.font-color-3F {
  color: #3f536e;
}

.font-color-009 {
  color: #009688;
}

.font-weight-500 {
  font-weight: 500;
}

.font-weight-bold {
  font-weight: bold;
}

.border-box {
  box-sizing: border-box;
}

.content-box {
  box-sizing: content-box;
}

.overflow {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.overflowNoDot {
  display: block;
  overflow: hidden;
}

.only_overflow {
  overflow: hidden;
}

.discountsPriceLine {
  text-decoration: line-through;
}

.border-bottom-Line {
  border-bottom: 1upx solid #ededed;
}

.decoration {
  text-decoration: line-through;
}

.anonymous {
  margin-top: 25upx;

  .uni-checkbox-input {
    border-color: #c5aa7b !important;
    width: 30upx;
    height: 30upx;
  }

  .uni-checkbox-input-checked:before {
    font-size: 30upx !important;
  }

  .uni-checkbox-input-checked {
    background: #c5aa7b;
  }
}

.footprint {
  .itemList {
    .uni-checkbox-input {
      border-color: #c5aa7b !important;
      width: 36upx;
      height: 36upx;
      border-radius: 50%;
      margin-right: 20upx;
    }

    .uni-checkbox-input-checked:before {
      font-size: 36upx !important;
    }

    .uni-checkbox-input-checked {
      background: #c5aa7b;
    }
  }
}

.itemInfo {
  uni-slider {
    margin: 0;

    .uni-slider-thumb {
      display: none;
    }

    .uni-slider-handle-wrapper {
      height: 18upx;
      border-radius: 0;
      border: 1upx solid #ff736c;
    }

    .uni-slider-track {
      border-radius: 0;
    }

    .uni-slider-tap-area {
      flex: 0 0 70%;
      padding: 0;
    }
  }
}

.uni-modal {
  padding: 20rpx;
  box-sizing: border-box;
}

uni-modal .uni-modal__ft:after {
  border-top: none;
}

uni-modal .uni-modal__btn {
  color: #333333;
  border: 2rpx solid #333333;
  font-weight: 400;
  margin: 0 10rpx;
  font-size: 28rpx;
}

.uni-tabbar .uni-tabbar__reddot {
  background: #ff0000;
  color: #ffffff;
}

uni-checkbox:not([disabled]) .uni-checkbox-input:hover {
  border-color: #c5aa7b !important;
}

.u-arrow {
  display: inline-block;
  width: 20rpx;
  height: 20rpx;
  border-top: 1rpx solid #999;
  border-right: 1rpx solid #999;
}

.u-arrow-up {
  transform: rotate(-45deg);
}

.u-arrow-down {
  transform: rotate(135deg);
}

.u-arrow-left {
  transform: rotate(-135deg);
}

.u-arrow-right {
  transform: rotate(45deg);
}

.uni-picker-container .uni-picker-action.uni-picker-action-confirm {
  color: #c5aa7b;
}

.u-drawer-content {
  //border-radius: 0 !important;
}

// 触底样式
.reachBottom {
  margin-top: 30rpx;
  display: flex;
  flex-direction: column;
  align-items: center;

  .reach-icon {
    width: 150rpx;
    height: 150rpx;
  }

  .reach-text {
    margin: 20rpx 0;
    color: #cccccc;
  }
}

.skeleton-box{
  background-color: #f0f2f5;
  border-radius: 10rpx;
}
</style>
<style>

uni-page-body {
  height: 100%;
}
</style>
